package com.org.controller;

import com.org.utils.AliOssUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.UUID;

/**
 * ClassName: FileUploadController
 * Package: com.org.controller
 * Description:
 *
 * @Author Origin
 * @Create 2024/1/29 22:03
 * @Version 1.0
 */
@RestController
@CrossOrigin//支持跨域
public class FileUploadController {

    @PostMapping("/upload")
    public String upload(MultipartFile file) throws Exception {

        //把文件的内容存储到本地磁盘上
        String originalFilename = file.getOriginalFilename();
        //保证文件名字唯一
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        //file.transferTo(new File("D:\\文件\\java学习\\SpringBoot入门\\files\\" + filename));
        String url = AliOssUtil.uploadFile(filename, file.getInputStream());
//        System.out.println(url);
        return url;
    }
}
